package com.ocom.payconfig.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ocom.common.entity.payconfig.EntityVoucherData;
import com.ocom.common.request.callpackage.GetPackagePayDetailResponse;
import com.ocom.common.vo.payconfig.CouponsDataListRVo;
import com.ocom.common.vo.payconfig.CouponsNumberRVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Date;
import java.util.List;

@Mapper
public interface IDaoVoucherData extends BaseMapper<EntityVoucherData> {

    @Select({"<script>",
            " SELECT\n" +
                    "\tvd.com_id,\n" +
                    "\tvd.p_id,\n" +
                    "\tvd.order_no,\n" +
                    "\tvd.voucher_type_id,\n" +
                    "\tvt.voucher_type_name,\n" +
                    "\tvd.number,\n" +
                    "\tvd.used_number,\n" +
                    "\tvd.attribute,\n" +
                    " \tvt.cons_type,\n" +
                    " \tvt.par_value,\n" +
                    "\tvd.data_status\n" +
                    " FROM\n" +
                    "\tvoucher_data vd\n" +
                    " LEFT JOIN voucher_type vt ON vd.com_id = vt.com_id\n" +
                    " AND vd.voucher_type_id = vt.voucher_type_id\n" +
                    " WHERE\n" +
                    "\tvd.com_id = #{comId}\n" +
                    " AND vd.p_id = #{pId}\n" +
                    " AND vd.order_no = #{orderNo}\n" +
                    " ORDER BY\n" +
                    "\tvd.id DESC" +
                    "</script>"})
    List<GetPackagePayDetailResponse> getPackagePayDetail(Long comId, Long pId, String orderNo);

    @Select({"<script>",
            " INSERT INTO voucher_data (\n" +
                    "\tcom_id,\n" +
                    "\tp_id,\n" +
                    "\tvoucher_type_id,\n" +
                    "\tattribute,\n" +
                    "\torder_no,\n" +
                    "\tvoucher_no,\n" +
                    "\tdata_status,\n" +
                    "\tnumber,\n" +
                    "\tused_number,\n" +
                    "\tstart_date,\n" +
                    "\tstop_date,\n" +
                    "\tgrant_time,\n" +
                    "\tcreate_time,\n" +
                    "\tcreate_userid\n" +
                    ") SELECT\n" +
                    "\t#{comId},\n" +
                    "\t#{pId},\n" +
                    "\tpv.voucher_type_id,\n" +
                    "\tvt.attribute,\n" +
                    "\t#{orderNo},\n" +
                    "\tconcat(#{orderNo}, pv.voucher_type_id, 'P', #{pId}),\n" +
                    "\t4,\n" +
                    "\tpv.number,\n" +
                    "\t0,\n" +
                    "\t#{startDate},\n" +
                    "\t#{stopDate},\n" +
                    "\tNOW(),\n" +
                    "\tNOW(),\n" +
                    "\t#{pId}\n" +
                    " FROM\n" +
                    "\tpackage_voucher pv\n" +
                    " LEFT JOIN voucher_type vt ON pv.com_id = vt.com_id\n" +
                    " AND pv.voucher_type_id = vt.voucher_type_id\n" +
                    " WHERE\n" +
                    "\tpv.com_id = #{comId}\n" +
                    " AND pv.package_type_id = #{packageTypeId}\n" +
                    " AND vt.status = 0" +
                    "</script>"})
    Integer batchSaveByPackageVoucher(Long comId, Long pId, String orderNo, Date startDate, Date stopDate, Long packageTypeId);

    @Select("<script> " +
            "        SELECT v.id AS vId, v.p_id AS pId , p.p_code AS pCode , p.p_name AS pName , p.dept_id AS deptId , a.acc_type_id AS accType,\n" +
            "                  v.voucher_no AS voucherNo , p.p_tags AS pTags, v.number AS voucherNumber, v.used_number AS usedNumber , v.grant_time as grantTime ,\n" +
            " DATE_FORMAT(v.start_date, '%Y-%m-%d') AS startDate , \n" +
            " DATE_FORMAT(v.stop_date, '%Y-%m-%d') AS stopDate" +
            "        FROM voucher_data v\n" +
            "                 LEFT JOIN personnel_info p ON p.p_id = v.p_id \n" +
            "                 LEFT JOIN xf_account_info a ON a.p_id = v.p_id\n" +
            "        WHERE v.com_id = #{comId} AND v.order_no = #{orderNo}\n" +
            "            <if test=\"deptIds !=null and deptIds.size() > 0\">\n" +
            "                   AND p.dept_id IN  " +
            "           <foreach collection='deptIds' item='item' open='(' separator=',' close=')'>" +
            "               #{item}" +
            "           </foreach>" +
            "            </if>\n" +
            "            <if test=\"accType != null and accType > 0\">\n" +
            "                AND a.acc_type_id = #{accType}\n" +
            "            </if>\n" +
            "            <if test=\"tag != null and tag !=''\">\n" +
            "                AND p.tag LIKE CONCAT('%',#{tag,jdbcType=VARCHAR},'%')\n" +
            "            </if>\n" +
            "            <if test=\"pCode != null and pCode !=''\">\n" +
            "                AND p.p_code = #{pCode,jdbcType=VARCHAR}\n" +
            "            </if>\n" +
            "            <if test=\"pName != null and pName !=''\">\n" +
            "                AND p.p_name LIKE CONCAT('%',#{pName,jdbcType=VARCHAR},'%')\n" +
            "            </if>\n" +
            "        ORDER BY pCode , pId" +
            "</script>")
    List<CouponsDataListRVo> couponsDataList(@Param("comId") Long comId, @Param("orderNo") String orderNo, @Param("deptIds") List<String> deptIds, @Param("accType") int accType, @Param("pCode") String pCode, @Param("pName") String pName, @Param("tag") String tag, @Param("voucherTypeId") int voucherTypeId);


    @Select("SELECT\torder_no AS orderNo , COUNT(p_id) AS personNumber ,\n" +
            "SUM(NUMBER)AS voucherNumber , SUM(used_number) AS usedNumber\n" +
            "FROM voucher_data \n" +
            "WHERE com_id = #{comId} AND order_no = #{orderNo}\t\n" +
            "GROUP BY order_no")
    CouponsNumberRVo couponsNumber(Long comId, String orderNo);
}
